package servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import pojo.EDeviceState;
import tools.Translator;
import utils.db.impl.EditDevice;

/**
 * Servlet implementation class ChangeStatusServlet
 */
@WebServlet("/ChangeStatusServlet")
public class ChangeStatusServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String deviceState =  request.getParameter("state");
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		int deviceId = Integer.parseInt(request.getParameter("deviceId"));
		PrintWriter out = response.getWriter();
		
		if(deviceState!=null && !deviceState.equals("0") && deviceId!=0){
			EDeviceState state = EDeviceState.valueOf(deviceState);
			EditDevice edit = new EditDevice();
			edit.changeStatus(deviceId, state);
			out.println("Zmieniono status urządzenia o id="+deviceId + " na " + Translator.toPolish(deviceState));
		}else{
			out.println("Problem przy zmianie statusu");
		}
		
		
	}


}
